function selectSubject() {
    var _this = this;
    var page;
    _this.index = -1;
    var items;
    _this.init = function (page_person) {
        page = page_person;
        _this.initTable();
        _this.initEvent();

    };

    _this.initEvent = function(){
        var subjectIdAll = [];
        var object = {};
        /**
         * 全选事件
         */
        $("#checkAll").click(function () {
                if (this.checked) {
                    $("#subject_table :checkbox:not(:disabled)").prop("checked", true);

                    $("#subject_table :checkbox:not(:disabled)").each(function () {
                        if (!object[$(this).val()]) {
                            object[$(this).val()] = 1;
                            subjectIdAll.push($(this).val());
                        }
                    });
                } else {
                    $("#subject_table :checkbox:not(:disabled)").prop("checked", false);
                    if (subjectIdAll.length > 0) {
                        for (var a = 0; a < subjectIdAll.length; a++) {
                            $("#subject_table :checkbox:not(:disabled)").each(function () {
                                if ($(this).val() === subjectIdAll[a]) {
                                    delete object[$(this).val()];
                                    subjectIdAll.splice(a, 1);
                                }
                            })
                        }
                    }
                }
            }
        );

        /**
         * 单选事件
         */
        $("#subject_table").on("click", "input[type=checkbox]", function () {
            if ($(this).is(":checked")) {
                if (!object[$(this).val()]) {
                    object[$(this).val()] = 1;
                    subjectIdAll.push($(this).val());
                }
            } else {
                if (subjectIdAll.length > 0) {
                    for (var a = 0; a < subjectIdAll.length; a++) {
                        if ($(this).val() === subjectIdAll[a]) {
                            delete object[$(this).val()];
                            subjectIdAll.splice(a, 1);
                        }
                    }
                }
            }
        });

        // 确定 (保存角色主题关联关系)
        $("#btn_sure").click(function () {
            var param = {
                'trainRoleId': $("#trainRoleId").val(),
                'trainRoleName': $("#trainRoleName").val(),
                'subjectIds': subjectIdAll.toString()
            }
            $.ajax({
                url: appPath + "/admin/matrix/saveMatrix",
                type: 'post',
                data: param,
                dataType: "json",
                success: function (data) {
                    var code = data.code;
                    if(code == 10000){
                        // 关闭弹窗，刷新列表
                        layer.msg("操作成功",{icon:1,time:1000}, function () {
                            _this.closeDialog();
                            parent.matrix_role_subject.initTable();
                        })
                    }
                }
            });
        })
    }

    _this.initTable = function () {
        var list_url = appPath + "/admin/trainSubject/paging";
        page.init("subject_form", list_url, "subject_table", "subject_page", 1, 10);
        page.goPage(1);
        page.list = function (dataList) {
            $("#all").attr("checked", false);
            var len = dataList.length;
            items = dataList;
            var inner = "", item;
            for (var i = 0; i < len; i++) {
                // 组装数据
                item = dataList[i];
                inner += '<tr>';
                inner += '<td><input type="checkbox" name="ckbox"  value="' + item.intTrainSubjectId + '" /></td>';
                inner += '<td>';
                inner += '<span>' + item.varSubjectName + '</span>';
                inner += '</td>';
                inner += '<td>' + item.courseCount + '</td>';
                inner += '<td>' + parseFullDate(item.datOperDate) + '</td>';
                inner += '</tr>';
            }
            return inner;
        }
    };

    // 搜索
    _this.search = function () {
        _this.initTable();
    };

    // 全部
    _this.searchAll = function () {
        $("#subject_form").find("input[id=subjectName]").val("");
        _this.initTable();
    };

    function parseFullDate(str) {
        if(!str) return "";
        if(str.length<=10) return "";
        return str.substring(0,10);
    }

    //关闭弹窗
    _this.closeDialog =function () {
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    }
}


var selectSubject = new selectSubject();